package org.ratingviewer.ui;

import java.awt.*;
import java.text.MessageFormat;
import java.util.List;

import javax.swing.*;

import org.ratingviewer.RatingViewer;
import org.ratingviewer.data.Player;
import org.ratingviewer.resources.Resources;
import org.ratingviewer.ui.action.*;

public class RatingViewerMainFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	private final RatingViewer ratingViewer;

	private JDesktopPane desktopPane;

	public RatingViewerMainFrame(RatingViewer ratingViewer) throws HeadlessException {
		super(Resources.getInstance().getString("frame.main.title"));

		if (ratingViewer == null)
			throw new IllegalArgumentException("RatingViewer can't be null.");
		this.ratingViewer = ratingViewer;

		initGUI();
		// TODO Auto-generated constructor stub
	}

	private void initGUI() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		JPanel contentPane = (JPanel) getContentPane();
		contentPane.setLayout(new BorderLayout(5, 5));

		desktopPane = createDesktopPane();
		contentPane.add(new JScrollPane(desktopPane), BorderLayout.CENTER);

		JMenuBar menuBar = createMenuBar(desktopPane);
		setJMenuBar(menuBar);

		// TODO Auto-generated method stub

		Resources resources = Resources.getInstance();
		Image mainIcon = resources.getImage(Resources.IMAGE_MAINICON);
		setIconImage(mainIcon);
	}

	private JDesktopPane createDesktopPane() {
		JDesktopPane desktopPane = new ScrollableJDesktopPane();
		desktopPane.setDoubleBuffered(true);
		desktopPane.setDragMode(JDesktopPane.LIVE_DRAG_MODE);

		Resources resources = Resources.getInstance();
		JInternalFrame searchFrame = new JInternalFrame(resources.getString("frame.search.title"), false, false,
				false, true);
		searchFrame.setFrameIcon(new ImageIcon(resources.getImage(Resources.IMAGE_MAINICON)));
		desktopPane.add(searchFrame);

		JPanel contentPane = (JPanel) searchFrame.getContentPane();
		contentPane.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
		contentPane.setLayout(new GridBagLayout());

		JTextField playerNameField = new JTextField();
		Dimension prefSize = playerNameField.getPreferredSize();
		playerNameField.setPreferredSize(new Dimension(Math.max(250, prefSize.width), prefSize.height));

		JButton searchButton = new JButton(new ActionSearch(ratingViewer, playerNameField, searchFrame));

		contentPane.add(playerNameField, new GridBagConstraints(0, 0, 1, 1, 1., 0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 5), 0, 0));
		contentPane.add(searchButton, new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

		searchFrame.pack();
		searchFrame.show();

		return desktopPane;
	}

	private JMenuBar createMenuBar(JDesktopPane desktopPane) {
		JMenuBar menuBar = new JMenuBar();

		Resources resources = Resources.getInstance();

		JMenu menu = new JMenu(resources.getString("menu.file.label"));
		menu.setMnemonic(resources.getChar("menu.file.mnemonic"));
		menuBar.add(menu);

		JMenuItem item = new JMenuItem(new ActionExit(this));
		menu.add(item);

		menu = new JMenu(resources.getString("menu.window.label"));
		menu.setMnemonic(resources.getChar("menu.window.mnemonic"));
		menuBar.add(menu);

		item = new JMenuItem(new ActionShowInternalFrame("item.showSearchFrame",
				resources.getString("frame.search.title"), desktopPane));
		menu.add(item);

		menu = new JMenu(resources.getString("menu.help.label"));
		menu.setMnemonic(resources.getChar("menu.help.mnemonic"));
		menuBar.add(menu);

		// TODO Auto-generated method stub
		return menuBar;
	}

	public void showError(String messageItem, Exception ex, Object... args) {
		Resources resources = Resources.getInstance();
		String message = resources.getString(messageItem);

		if (args != null)
			message = MessageFormat.format(message, args);

		JOptionPane.showInternalMessageDialog(this, message, resources.getString("frame.error.title"),
				JOptionPane.ERROR_MESSAGE);
	}

	public void showPlayers(String playerName, List<Player> players) {
		Resources resources = Resources.getInstance();
		String title = MessageFormat.format(resources.getString("frame.searchResult.title"), playerName);
		
		JInternalFrame resultsFrame = new JInternalFrame(title, true, true, true, true);
		resultsFrame.setFrameIcon(new ImageIcon(resources.getImage(Resources.IMAGE_MAINICON)));
		desktopPane.add(resultsFrame);

		resultsFrame.pack();
		resultsFrame.show();
		// TODO Auto-generated method stub

	}
}
